<template>
  <div class="app-container" style="cursor:default;">
    <!--工具栏-->
    <div class="head-container">
      <eHeader :dict="dict"/>
      <!-- <div>
     <crudOperation>
       <el-button
          v-permission="['admin', 'inventory:import']"
          slot="right"
          class="filter-item"
          type="primary"
          icon="el-icon-add"
          size="mini"
          @click="onBatchImport(0)"
        >
        新增患者
        </el-button>
       <el-button
          v-permission="['admin', 'inventory:import']"
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-add"
          size="mini"
          @click="onBatchImport(1)"
        >
        批量导入患者
        </el-button>
        </crudOperation> 
      </div> -->
      
       
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" >
      <el-table-column v-if="columns.visible('name')" prop="name" label="姓名" />
      <el-table-column v-if="columns.visible('sex')" prop="sex" label="性别">
        <template slot-scope="scope">{{ getPatientSex(scope.row.sex) }}</template>
      </el-table-column>
      <el-table-column v-if="columns.visible('age')" prop="age" label="年龄"/>
      <el-table-column v-if="columns.visible('phone')" prop="phone" label="手机号"/>
      <!-- <el-table-column v-if="columns.visible('membershipName')" prop="membershipName" label="会员级别"/> -->
      <!-- <el-table-column v-if="columns.visible('jzCount')" prop="jzCount" label="就诊次数"/>
      <el-table-column v-if="columns.visible('totalFee')" prop="totalFee" label="消费总额(零售)"/> -->
      <el-table-column v-if="columns.visible('illness')" prop="illness" label="慢性病史"/>
      <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="登记时间"/> 

      <!-- <el-table-column v-if="columns.visible('sort')" prop="sort" label="标签" width="300px" align="center">
        <template slot-scope="scope">
          <template v-for="item in scope.row.labelDtos">
            <span style="border-radius: 40px;border: 1px solid #A7C9F1;padding:2px 5px;background-color:#F1F7FB;color:#A7C9F1"
              :key="item.id"
            >{{item.labelName}}</span>
          </template>
           <el-popover
             placement="bottom"
             width="300"
             trigger="click">
              <template v-for="dots in labelDtosList">
                <span :key="dots.id" @click="onClickLabelsSubmie(dots,scope.row)" style="border-radius: 40px;border: 1px solid #A7C9F1;padding:0px 5px;background-color:#F1F7FB;color:#A7C9F1;">{{dots.labelName}}</span>
               
              </template>
            
             <i  class="el-icon-edit" style="margin-left:10px;" slot="reference"  @click="onClickItemEdit(scope.row)"></i>
           </el-popover>
          
        </template>
      </el-table-column> -->

      <!-- <el-table-column
        v-permission="['admin','user:edit','user:del']"
        label="操作"
        width="230px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <template v-for="item in scope.row.btns">
            <el-button
              :key="item.id"
              type="text" 
              @click="onRowHandle(item)"
            >{{ item.text }}</el-button>
          </template>
        </template>
      </el-table-column> -->
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--表单渲染-->
    <template v-if="showUserMsg">
        <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="showUserMsg"
    :title="crud.status.title"
    width="60%"
  >
          <el-form ref="form" :inline="true" :model="uform" :rules="rules" size="small" label-width="66px">
            <div style="background-color:#A7C9F1;height:30px;margin-bottom:20px;color:#fff;padding:6px">
              <span>基本信息</span>
            </div>
            <div>
            <el-form-item label="姓名" prop="name" label-width="100px">
              <el-input v-model="uform.name" />
            </el-form-item>
            <el-form-item label="性别" label-width="100px">
              <el-radio-group v-model="uform.sex" style="width: 178px">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="年龄" prop="age" label-width="100px">
              <el-input v-model.number="uform.age" />
            </el-form-item>
            </div>
            <div>
            <el-form-item label="手机号码" prop="phone" label-width="100px">
              <el-input v-model.number="uform.phone" />
            </el-form-item>
            <el-form-item label="出生日期" prop="birthday" label-width="100px">
         <el-date-picker
          v-model="uform.birthday"
          type="date"
          size="small"
          class="filter-item charges-date-picker"
          placeholder="出生日期"
          value-format="yyyy-MM-dd"
          style="width: 178px"
        >
        </el-date-picker>
      </el-form-item>
            <el-form-item label="身份证号" prop="idCard" label-width="100px">
              <el-input v-model="uform.idCard" />
            </el-form-item>
            </div>
<div>
            <el-form-item label="家庭住址" prop="address" label-width="100px">
              <el-input v-model="uform.address" />
            </el-form-item>
             <el-form-item label="患者来源" prop="source" label-width="100px">
              <el-select v-model="uform.source" style="width: 178px" placeholder="患者来源">
                <!-- <el-option
                  v-for="(item, index) in accountsourceTypes"
                  :key="item.name + index"
                  :label="item.name"
                  :value="item"
                /> -->
                <el-option
            v-for='item in accountsourceTypes'
            :key='item.id'
            :label='item.text'
            :value='item.id'
          />
              </el-select>
            </el-form-item>
</div>

 <div style="background-color:#A7C9F1;height:30px;margin-bottom:20px;color:#fff;padding:6px">
              <span>其他信息</span>
            </div>

          <div>
             <el-form-item label="婚姻状况" prop="membershipType" label-width="100px">
              <el-select v-model="form.membershipType" style="width: 178px" placeholder="婚姻状况">
                <!-- <el-option
                  v-for="(item, index) in maritalstatusTypes"
                  :key="item.name + index"
                  :label="item.name"
                  :value="item"
                /> -->
                 <el-option
            v-for='item in maritalstatusTypes'
            :key='item.id'
            :label='item.text'
            :value='item.id'
          />
              </el-select>
            </el-form-item>

            <el-form-item label="民族" prop="nation" label-width="100px">
              <el-select v-model="form.nation" style="width: 178px" placeholder="请选择">
                <!-- <el-option
                  v-for="(item, index) in nationTypes"
                  :key="item.name + index"
                  :label="item.name"
                  :value="item"
                /> -->
                <el-option
            v-for='item in nationTypes'
            :key='item.id'
            :label='item.text'
            :value='item.id'
          />
              </el-select>
            </el-form-item>
            <el-form-item label="会员类型" prop="membershipType" label-width="100px">
              <span style="color:#DC143C">【请前往会员中心设置】</span>
            </el-form-item>
          </div>

          <div>
            <el-form-item label="微信号" prop="wechat" label-width="100px">
              <el-input v-model="form.wechat" />
            </el-form-item>
 <el-form-item label="职业" prop="workUnit" label-width="100px">
             <el-select
          v-model='uform.workUnit'
          clearable
          size='small'
          placeholder='职业'
          class='filter-item'
          style='width: 178px'
          @change="crud.toQuery"
        >
          <el-option
            v-for='item in workunitTypes'
            :key='item.id'
            :label='item.text'
            :value='item.id'
          />
        </el-select>
            </el-form-item>
            <el-form-item label="工作单位" prop="workUnit" label-width="100px">
              <el-input v-model="form.workUnit" />
            </el-form-item>
          </div>

          <div>
            <el-form-item label="慢性病" label-width="100px">
              <el-radio-group v-model="form.isChronicDisease" style="width: 178px">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item> 

            <el-form-item label="慢性病史" prop="illness" label-width="100px">
              <el-input v-model="form.illness" />
            </el-form-item>

            <el-form-item label="备注" prop="remark" label-width="100px">
              <el-input v-model="form.remark" />
            </el-form-item>
          </div>

          </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        type="text"
        @click="showUserMsg = false"
      >
        取消
      </el-button>
      <el-button
        type="primary"
        @click="onClickSumbit"
      >
        确认
      </el-button>
    </div>
  </el-dialog>
    </template>
    
    <el-dialog
          width="600px"
          append-to-body
          :close-on-click-modal="false"
          :visible.sync="showBatchImport"
          title="批量导入患者"
        >
          <batchImport 
            @cancel="showBatchImport=false"
            @success="onBatchImportSuccess"
          />
        </el-dialog>
  </div>
</template>

<script>
// import crudJob from '@/api/outpatientService/outuser'
import { getPatientSex, formatMoney } from "@/utils/index";
import { add , edit,lableSubmit, getLabels } from '@/api/outpatientService/outuser'
import eHeader from './module/header'
import eForm from './module/form'
import batchImport from "./module/batchImport";
import CRUD, { presenter , header, form} from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { mapGetters } from 'vuex';
// crud交由presenter持有
// const crud = CRUD({
//   title: '患者信息',
//   url: 'api/use/getList',
//   sort: ['sort,asc', 'id,desc'],
//   crudMethod: { ...crudJob }
// })

let crud = CRUD({
  query: { endTime: null,memberId:null, startTime: null, pharmacyId: null, sort: 'asc', 
           name: null },
  hideColumns: [],
  url: "account/getList",
  sort: [],
  title: "患者信息"
});

let defaultForm = {

}
export default {
  name: 'outUser',
  components: { eHeader, eForm, crudOperation, pagination, udOperation, batchImport },
  mixins: [presenter(crud), header(),form(defaultForm)],
  // 数据字典
  dicts: ['job_status'],
  data() {
    return {
      labelDtosList:[],
      permission: [
        {
          type: "add",
          permission: ["admin","user:add"],
          name: "新增患者",
          position: 1
        }
      ],

      uform: {	
  address: "",
	age: 0,
	birthday: null,
	createTime: "",
	id: null,
	idCard: "",
	illness: "",
	isChronicDisease: 1,
	maritalStatus: '',
	membershipType: "0",
	name: "",
	nation: "",
	occupation: "",
	pharmacyId: "",
	phone: "",
	remark: "",
	sex: 1,
	source: "",
	status: 0,
	wechat: "",
	workUnit: ""},
  rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
        ],
        dept: { required: true, message: '所属部门不能为空', trigger: 'select' }
      },

    showBatchImport: false,
    showUserMsg: false,
    }
  },
    computed: {
    ...mapGetters(["workunitTypes"]),
    ...mapGetters(["nationTypes"]),
    ...mapGetters(["maritalstatusTypes"]),
    ...mapGetters(["accountsourceTypes"]),
  },
     mounted() {
     
  },
  methods: {
    getPatientSex,
        onClickSumbit(){
          if(this.uform.id != null){
    var dataform = {	
  address: this.uform.address,
	age: this.uform.age,
	birthday: this.uform.birthday,
	createTime: this.uform.createTime,
	id: this.uform.id,
	idCard: this.uform.idCard,
	illness: this.uform.illness,
	isChronicDisease: this.uform.isChronicDisease,
	maritalStatus: this.uform.maritalStatus,
	membershipType: this.uform.membershipType,
	name: this.uform.name,
	nation: this.uform.nation,
	occupation: this.uform.occupation,
	pharmacyId: this.uform.pharmacyId,
	phone: this.uform.phone,
	remark: this.uform.remark,
	sex: this.uform.sex,
	source: this.uform.source,
	status: this.uform.status,
	wechat: this.uform.wechat,
	workUnit: this.uform.workUnit};
  edit(dataform).then(res => {
        // this.data = res;
        this.showUserMsg = false;
        this.crud.toQuery();
      });
          }else{
  add(this.uform).then(res => {
        this.data = res;
        this.showUserMsg = false;
        this.crud.toQuery();
      });
     }

    },
    onClickLabelsSubmie(data,row){
      
      var formdata = {
        accountId:row.id,
        labelId:data.id,
        createTime:'',
        id:data.rid != null? data.rid:null
      };
       lableSubmit(formdata).then(res => {
         this.crud.toQuery();
      });
    },
    onClickItemEdit(e){
       if(e.labelDtos.length> 0){
         e.labelDtos.forEach(val => {
 
      let contentobj = this.labelDtosList.find((item) => {
        return item.id === val.labelId;
      });
      contentobj.accountId = val.accountId;
      contentobj.rid = val.id;
        })
       }
    },
        onOperateRow(id,operate) {
          this.uform = operate;
    },
        onRowHandle(btn) {
          
          let flage = btn.params[0];
          if(flage == 1){
            this.showUserMsg = true;
          }
      btn.handle.apply(this, btn.params)
    },

        [CRUD.HOOK.dataLoadSuccess](crud, data) {
         this.labelDtosList = [];
            getLabels().then(res => {
              res.forEach(item=>{
      this.labelDtosList.push(item)
              })
      });
      data.forEach(item => {
        item.btns = this._operateBtns(item);
      });
    },
    [CRUD.HOOK.afterRefresh]() {
      
    },
        _operateBtns(item) {
      let res = [];
      res.push({ text: "编辑", handle: this.onOperateRow, params: [1,item] });
    res.push({ text: "预约挂号", handle: this.onOperateRow, params: [2,item] });
    res.push({ text: "历史病历", handle: this.onOperateRow, params: [3,item] });

      return res;
    },
       onBatchImportSuccess() {
      this.showBatchImport=false;
      this.crud.toQuery();
      this.searchCountByDrugType();
    },
     onBatchImport(e) {
       if(e == 0){
this.showUserMsg = true;
this.uform = {	
  address: "",
	age: 0,
	birthday: null,
	createTime: "",
	id: null,
	idCard: "",
	illness: "",
	isChronicDisease: 1,
	maritalStatus: '',
	membershipType: "0",
	name: "",
	nation: "",
	occupation: "",
	pharmacyId: "",
	phone: "",
	remark: "",
	sex: 1,
	source: "",
	status: 0,
	wechat: "",
	workUnit: ""};
       }else{
this.showBatchImport = true;
       }
      
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.job_status[val] + '" ' + data.name + '岗位, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crud.crudMethod.edit(data).then(() => {
          crud.notify(this.dict.label.job_status[val] + '成功', 'success')
        }).catch(err => {
          data.enabled = !data.enabled
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
